import { v1 as uuidv1 } from 'uuid'
import moment from 'moment'
import SnowflakeId from 'snowflake-id'

export function uuid() {
    return uuidv1().replace(/-/g, '')
}

export function timestamp(time?: moment.MomentInput) {
    return Number(moment(time).format('x'))
}

export function timestampFormat(time?: moment.MomentInput) {
    return moment(time).format('YYYY-MM-DD HH:mm:ss')
}
export function snowflake(num?: number) {
    const snowflake = new SnowflakeId()
    let arr = []
    for (let i = 0; i < num; i++) {
        arr.push(snowflake.generate())
    }
    return num ? arr : snowflake.generate()
}
export function sleep(time: number) {
    return new Promise<void>((resolve) => {
        setTimeout(() => {
            resolve()
        }, time)
    })
}
/**
 * @description: 列表转树
 * @param {any} list
 * @param {string} parentKey 父级键
 * @param {string} childKey 子级键
 * @param {any} startValue 起始父级值
 * @return {*}
 */
export function list2tree(list: any[], parentKey: string, childKey: string, startValue: any) {
    const parents = list.filter((o) => o[parentKey] === startValue)
    for (const p of parents) {
        p.children = this.listToTree(list, p[childKey])
    }
    return parents
}
